package com.lkc.controller;

import com.lkc.bean.ShoppingCar;
import com.lkc.service.ShoppingCarService;
import com.lkc.util.Page;
import com.lkc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin  // 允许跨域访问
@RequestMapping("shoppingCar") // 路径前缀
public class ShoppingCarController {

    @Autowired
    private ShoppingCarService shoppingCarService;

    // 新增购物车记录
    @PostMapping
    public Result add(@RequestBody ShoppingCar shoppingCar) {
        ShoppingCar obj = shoppingCarService.add(shoppingCar);
        return Result.success(obj);
    }

    // 编辑购物车记录
    @PutMapping
    public Result edit(@RequestBody ShoppingCar shoppingCar) {
        ShoppingCar obj = shoppingCarService.edit(shoppingCar);
        return Result.success(obj);
    }

    // 删除购物车记录（逻辑删除）
    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id) {
        int obj = shoppingCarService.del(id);
        return Result.success(obj);
    }

    // 根据ID查询购物车记录
    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id) {
        ShoppingCar obj = shoppingCarService.one(id);
        return Result.success(obj);
    }

    // 查询所有购物车记录（可带条件）
    @GetMapping("all")
    public Result all(ShoppingCar shoppingCar) {
        List<ShoppingCar> obj = shoppingCarService.all(shoppingCar);
        return Result.success(obj);
    }

    // 分页查询购物车记录
    @GetMapping("page")
    public Result page(Page page, ShoppingCar shoppingCar) {
        shoppingCarService.page(page, shoppingCar);
        return Result.success(page);
    }
}
